package com.ebt.m.wiki;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;

import com.ebt.m.R;
import com.ebt.m.commons.widgets.view.EBTDialog;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.proposal_v2.utils.ProposalUtils;
import com.ebt.m.proposal_v2.mvp.presenter.MainItemViewPresenter;
import com.ebt.m.view.LineBreakLayout;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;

/**
 * Created by wanglu on 2017/10/18.
 */

public class SpecProductFilterHistory extends BaseListView<SpecProductHistoryPresenter> {

    public static final int TAG_VIEW_YTPE = 0;
    public static final int HISTORY_VIEW_TYPE = 1;
    public static final int BRAND_VIEW_TYPE = 2;

    private OnHistoryItemClick onHistoryItemClick;
    private int mFrom;
    private MainItemViewPresenter mainItemViewPresenter;

    public SpecProductFilterHistory(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SpecProductFilterHistory(Context context) {
        this(context, null);
    }

    public SpecProductFilterHistory(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mRefreshLayout.setRefreshNeeded(false);
    }

    public IView.OnClickListner onClickListner = new IView.OnClickListner() {
        @Override
        public void onClick(View view, Object... args) {
            switch (view.getId()) {
                case R.id.delete:
                    deleteHistorys();
                    break;
                case R.id.text:
                    if (onHistoryItemClick != null)
                        onHistoryItemClick.onItemClick((Brand) args[0], 0);
                    break;
                case R.id.content:
                    Brand brand = (Brand) args[0];
                    doBrandClick(brand.getProductId());
                    break;
            }
        }

        @Override
        public void onLongClick(View view, Object... args) {

        }
    };

    private void doBrandClick(int productId) {
        if (mFrom == 4) {
            if (mainItemViewPresenter == null) {
                mainItemViewPresenter = new MainItemViewPresenter(getContext());
            }
            mainItemViewPresenter.onItemClick(productId, null);
        } else if (mFrom == 2) {
            ProposalUtils.startMakingProposalFromDynamic(getContext(), productId);
        }
    }

    private void deleteHistorys() {
        EBTDialog ebtDialog = new EBTDialog();
        ebtDialog.setMessage("确定清空搜索记录吗？");
        ebtDialog.setRightButton("清空", v -> mPresenter.deleteHistory());
        ebtDialog.setLeftButton("取消", v -> ebtDialog.dismiss());
        ebtDialog.show(((AppCompatActivity) getContext()).getSupportFragmentManager(), getClass().getSimpleName());
    }

    @Override
    protected ISectionFactory createViewFactory() {
        return viewType -> {
            if (viewType == TAG_VIEW_YTPE) {
                ProductSearchHistoryTag productSearchHistoryTag = new ProductSearchHistoryTag(getContext());
                productSearchHistoryTag.setOnClickViewListener(onClickListner);
                return productSearchHistoryTag;
            } else if (viewType == HISTORY_VIEW_TYPE) {
                LineBreakLayout lineBreakLayout = new LineBreakLayout(getContext());
                lineBreakLayout.setOnClickViewListener(onClickListner);
                return lineBreakLayout;
            } else if (viewType == BRAND_VIEW_TYPE) {
                SectionSearchMaybeBrand maybeBrand = new SectionSearchMaybeBrand(getContext());
                maybeBrand.setOnClickViewListener(onClickListner);
                return maybeBrand;
            }
            return null;
        };
    }

    @Override
    protected SpecProductHistoryPresenter createPresenter() {
        return new SpecProductHistoryPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }

    @Override
    public void update(Object... args) {
        super.update(args);
        mFrom = (int) args[0];
        mPresenter.loadNew(args);
    }

    @Override
    protected boolean createFootView() {
        return false;
    }

    public interface OnHistoryItemClick {
        void onItemClick(Brand brand, int position);
    }

    public void setOnHistoryItemClick(OnHistoryItemClick onHistoryItemClick) {
        this.onHistoryItemClick = onHistoryItemClick;
    }

}
